#include "plook_u32s16u16n16_binca_s.h"
#include "binsearch_u32s16.h"
#include "rtwtypes.h"

uint32_T plook_u32s16u16n16_binca_s(int16_T u, const int16_T bp[], uint32_T
  maxIndex, uint16_T *fraction)
{
  uint32_T bpIndex;
  if (u <= bp[0U]) {
    bpIndex = 0U;
    *fraction = 0U;
  } else if (u < bp[maxIndex]) {
    int16_T bpLeftVar;
    bpIndex = binsearch_u32s16(u, bp, maxIndex >> 1U, maxIndex);
    bpLeftVar = bp[bpIndex];
    *fraction = (uint16_T)(((uint32_T)(uint16_T)(u - bpLeftVar) << 16) /
      (uint16_T)(bp[bpIndex + 1U] - bpLeftVar));
  } else {
    bpIndex = maxIndex;
    *fraction = 0U;
  }

  return bpIndex;
}
